package com.szpx.service.impl;

import com.szpx.entity.Comment;
import com.szpx.mapper.CommentMapper;
import com.szpx.service.CommentService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: Hudson
 * @Version: 1.0
 * @Date: 2024/4/8 下午8:37
 * @Description: TODO
 */
@Service
public class CommentServiceImpl implements CommentService {
	
	@Autowired
	private CommentMapper commentMapper;
	
	@Override
	public List<Comment> findByGoodsId(Comment comment){
		
		//构建查询条件
		Example example = creatExample(comment);
		
		return commentMapper.selectByExample(example);
	}
	
	@Override
	public Comment findById(Integer id) {
		Comment comment = commentMapper.selectByPrimaryKey(id);
		return comment;
	}
	
	@Override
	public void addComment(Comment comment) {
		commentMapper.insert(comment);
	}
	
	@Override
	public void updateComment(Comment comment) {
		commentMapper.updateByPrimaryKeySelective(comment);
	}
	
	@Override
	public void delComment(Integer id) {
		commentMapper.deleteByPrimaryKey(id);
	}
	
	public Example creatExample(Comment comment) {
		Example example = new Example(Comment.class);
		Example.Criteria criteria = example.createCriteria();
		if (comment != null) {
			//评论id
			if (!StringUtils.isEmpty(comment.getId())) {
				criteria.andEqualTo("id", comment.getId());
			}
	        //商品id
	        if (!StringUtils.isEmpty(comment.getGoodsid())) {
	           criteria.andEqualTo("goodsid", comment.getGoodsid());
			}
			// 评论详情
			if (!StringUtils.isEmpty(comment.getContext())) {
				criteria.andLike("context", "%" + comment.getContext() + "%");
			}
		}
		return example;
	}
	
}
